package aware.util.dao.oracle;


import java.sql.SQLException;
import java.util.Collection;

import javax.sql.RowSet;

import org.hibernate.Session;

import aware.util.dao.hibernate.HibernateUtil;
import aware.util.dao.oracle.OracleStoredProcedureExecutor;
import aware.common.dto.UserDTO;
import aware.common.exception.AwareException;
import aware.util.dao.UserDAO;

public class OracleUserDAO implements UserDAO {

	@Override
	public boolean deleteUser(UserDTO user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public UserDTO findUser(UserDTO user) throws AwareException {
		/*Connection conn=OracleDAOFactory.createConnection();
		CallableStatement csmt=null;
		
		String sql="{ call TEST_ARITRA_SAMPLE.ISVALIDUSER ( ? , ? , ? ) }";
		try {
			csmt=conn.prepareCall(sql);
			csmt.setString("AWAREUSERID", user.getAwareUserId());
			csmt.setString("AWAREPASSWORD", user.getAwarePassword());
			csmt.registerOutParameter("VALIDUSER", Types.VARCHAR);
			csmt.execute();
			System.out.println("Output:"+csmt.getString("VALIDUSER"));
			user.setValidUser(csmt.getString("VALIDUSER"));
		} catch (SQLException e1) {
			throw new AwareException(e1);
		} finally {
			DaoUtil.close(conn, csmt);
		}*/
		//Object obj=null;
		OracleStoredProcedureExecutor pe=new OracleStoredProcedureExecutor("TEST_ARITRA_SAMPLE","ISVALIDUSER");
		
		try {
			pe.execute(user);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//pe.test(user);
		try {
			while(user.getEmpResult()!=null && user.getEmpResult().next()) {
				System.out.print("Ename:="+user.getEmpResult().getString("ENAME"));
				System.out.println("\tEmp No:="+user.getEmpResult().getString("EMPNO"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if("FALSE".equalsIgnoreCase(user.getValidUser())) {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			
			session.save(user);
			session.getTransaction().commit();
		}
		return user;
	}

	@Override
	public int insertUser(UserDTO user) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public RowSet selectUsersRS(UserDTO user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<UserDTO> selectUsersTO(UserDTO user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean updateUser(UserDTO user) {
		// TODO Auto-generated method stub
		return false;
	}

	


}
